/**********************************************
 * 姓名：fanfutian
 * 时间：2023/11/9                             
 * 文件名：teacher.cpp  
 * 功能：实现teacher类中的成员函数
 *********************************************/
#include "teacher.h"

/*
 * 默认构造函数
 */
Teacher::Teacher(){
    this->initMapOrder();
}

/*
 * 有参构造函数
 */
Teacher::Teacher(int id,string name,string pwd){
   this->m_id = id;
   this->m_name = name;
   this->m_pwd = pwd;
   this->initMapOrder();
}

/*
 * 重写纯虚函数
 */
void Teacher::showMenu(){
    cout << "欢迎老师" << this->m_name << "登录！" << endl;
    while(true){
        int chose = 0;
        cout << "**************************" << endl;
        cout << "\t\t1、查看所有预约" << endl;
        cout << "\t\t2、审核预约" << endl;
        cout << "\t\t3、退出登录" << endl;
        cout << "*************************" << endl;
        cout << "请输入你的选择：";
        cin >> chose;
        switch(chose){
            case 1:
                this->showAllOrder();
                break;
            case 2:
                this->validOrder();
                break;
            case 3:
                cout << "成功退出登录！" << endl;
                return;
            default:
                cout << "输入错误！！" << endl;
                break;
        }
    }
}

/*
 * 查看所有预约
 */
void Teacher::showAllOrder(){
    if(this->m_tea_od.empty()){
        cout << "预约内容为空！" << endl;
        return;
    }
    // 遍历容器查找与当前用户相同的预约记录
    cout << "所有预约记录如下：" << endl;
    for(int i = 0; i < this->m_tea_od.size(); i++){
        cout << "预约学生:" << this->m_tea_od[i]["预约学生"] << "  " << "学号:" << this->m_tea_od[i]["学号"]  << "  " << "机房号:" << this->m_tea_od[i]["机房号"]  << "  "
             << "星期:" << this->m_tea_od[i]["星期"]  << "  " << "课节:" << this->m_tea_od[i]["课节"] << "  " ;
        string status = "状态:";
        if(this->m_tea_od[i]["状态"] == "1"){
            status += "审核中";
        }else if(this->m_tea_od[i]["状态"] == "2"){
            status += "预约成功";
        }else if(this->m_tea_od[i]["状态"] == "-1"){
            status += "预约失败";
        }else{
            status += "取消预约";
        }
        cout << status << endl;
    }
}

/*
 * 审核预约
 */
void Teacher::validOrder(){
    // 创建一个临时容器，保存状态为审核中的数据，方便修改
    vector<int> v;
    // 遍历容器显示预约中的信息
    int tmp = 0,count = 0;
    int i ;
    for(i = 0 ; i < m_tea_od.size(); i++){
        if(this->m_tea_od[i]["状态"] == "1"){
            if(tmp == 0)
                cout << "需要审核的信息如下：" << endl;
            count++;
            cout << count << "、";
            v.push_back(i);
            cout << "预约学生:" << this->m_tea_od[i]["预约学生"] << "  " << "学号:" << this->m_tea_od[i]["学号"]  << "  " << "机房号:" << this->m_tea_od[i]["机房号"]  << "  "
                 << "星期:" << this->m_tea_od[i]["星期"]  << "  " << "课节:" << this->m_tea_od[i]["课节"] << "  " << "状态:" << this->m_tea_od[i]["状态"] << endl;
            tmp = 1;
        }
    }
    if(tmp == 0){
        cout << "没有需要审核的信息！" << endl;
        return;
    }
    int hount;
    cout << "请选择需要进行审核的行号：";
    cin >> hount;
    while(true){
        if(hount <=0 || hount > count){
            cout << "输入错误，重新选择：";
            cin >> hount;
        }else{
            int status;
            cout << "是否通审核：1.通过   2.不通过   3.退出" << endl;
            cout << "请选择：";
            cin >> status;
            if(status == 1){
                this->m_tea_od[hount]["状态"] = "2";
                cout << "审核成功！" << endl;
            }else if(status == 2){
                this->m_tea_od[hount]["状态"] = "-1";
                cout << "审核失败！" << endl;
            }else if(status == 3){
                return;
            }
            this->updateInfor(status);
            break;
        }
    }
}

/*
 * 更新order文件信息
 */
void Teacher::updateInfor(int status) {
    ofstream ofs;
    ofs.open(ORDER_FILE, ios::trunc |ios::out);
    for (int i = 0; i < m_tea_od.size(); ++i) {
        ofs << "预约学生:" << m_tea_od[i]["预约学生"] << "  " << "学号:" << m_tea_od[i]["学号"] << "  " << "机房号:" << m_tea_od[i]["机房号"] << "  " << "星期:" << m_tea_od[i]["星期"] << "  " << "课节:" << m_tea_od[i]["课节"] << "  "  << "状态:"  << status;
        ofs << '\n';
    }
    ofs.close();
}


/*
 * 初始化预约容器
 */
void Teacher::initMapOrder() {
    ifstream ifs;
    ifs.open(ORDER_FILE,ios::in);
    if(!ifs.is_open()){
        cout << "文件不存在！" << endl;
        return;
    }
    Order od;
    while(ifs >> od.s_name && ifs >> od.s_id && ifs >> od.com_num && ifs >> od.week && ifs >> od.course && ifs >> od.state){
        // 创建一个小容器
        map<string,string> m;
        // 使用字符串查找进行字符分割
        this->strFindSub(m,od.s_name);
        this->strFindSub(m,od.s_id);
        this->strFindSub(m,od.com_num);
        this->strFindSub(m,od.week);
        this->strFindSub(m,od.course);
        this->strFindSub(m,od.state);
        // 将小的map容器放入大的map容器中
        this->m_tea_od.insert(make_pair(m_tea_od.size(),m));
    }
    ifs.close();
}

/*
 * 传入一个字符串进行指定条件分割
 */
void Teacher::strFindSub(map<string,string> &m,string& str){
    string key,value;
    int pos = str.find(':');
    // 判断是否结尾
    if(pos != -1){
        // substr参数1表示截取的起始位置，参数2表示截取的长度
        key = str.substr(0,pos);
        value = str.substr(pos+1,str.size()-pos-1);
        m.insert(make_pair(key,value));
    }
}